<template>
  <div class="login_container">
    <el-row>
      <!-- 
        element栅格布局span总共24个,
         xs:当屏幕小于768px :不占位置
       -->
      <el-col :span="9" :xs="0"></el-col>
      <el-col :span="6" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
          @keydown.enter="login"
        >
          <div class="title">
            <img class="logo" :src="setting.logo" alt="" />
            <h1>Education</h1>
          </div>
          <h2>欢迎来到--优易学</h2>
          <el-form-item prop="username">
            <el-input
              class="h-38"
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              class="h-38"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              size="large"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="9" :xs="0"></el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import setting from '@/setting'
import { User, Lock } from '@element-plus/icons-vue'
import { ElNotification } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import useUserStore from '@/store/modules/user'
import { getTime } from '@/utils/time'

let userStore = useUserStore()
let $router = useRouter()
let loginForm = reactive({ username: 'admin', password: '123456' })
let loading = ref(false)
let loginForms = ref()

// 登录回调函数
const login = () => {
  loginForms.value
    .validate()
    .then(() => {
      loading.value = true
      userStore
        .userLogin(loginForm)
        .then(() => {
          loading.value = false
          $router.push('/')
          //登录成功提示
          ElNotification({
            title: '成功',
            type: 'success',
            message: `Hi.${getTime()}好！`,
          })
        })
        .catch((err) => {
          loading.value = false
          ElNotification({
            title: '错误',
            type: 'error',
            message: err,
          })
        })
    })
    .catch(() => {
      ElNotification({
        type: 'warning',
        message: '填写数据不合法！',
      })
    })
}

//定义表单校验需要配置对象
const rules = {
  //规则对象属性:
  //required,代表这个字段务必要校验的
  //min:文本长度至少多少位
  //max:文本长度最多多少位
  //message:错误的提示信息
  //trigger:触发校验表单的时机 change->文本发生变化触发校验,blur:失去焦点的时候触发校验规则
  username: [
    {
      required: true,
      min: 2,
      max: 30,
      message: '账号长度至少2位',
      trigger: 'change',
    },
  ],
  password: [
    {
      required: true,
      min: 6,
      max: 15,
      message: '密码长度至少6位',
      trigger: 'change',
    },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: url('@/assets/images/bg.png') no-repeat;
  background-size: cover;

  .login_form {
    padding-top: 25%;
    text-align: center;
    .title {
      display: flex;
      align-items: center;
      justify-content: center;
      .logo {
        margin: 0 20px;
        width: 50px;
        height: 100%;
      }
      h1 {
        color: rgba(0, 0, 0, 0.85);
        font-size: 40px;
      }
    }

    h2 {
      color: rgba(0, 0, 0, 0.85);
      font-size: 20px;
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
